<template>
  <div class="login-container" v-bind:style="{backgroundImage: 'url(' + bg + ')',backgroundRepeat: 'no-repeat',backgroundSize: '100% 100%',}">

    <el-card class="login-form-layout">
      <el-form ref="loginForm" class="login-form" :rules="rules" :model="loginForm">
        <div class="login-title">
          <h1>Octoplus测试平台</h1>
        </div>
        <el-form-item prop="username">
          <el-input placeholder="请输入账号" prefix-icon="el-icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" prefix-icon="el-icon-lock" show-password v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-button type="primary" style="width:48%;margin-top:10px" @click="handleLogin()">登录</el-button>
        <el-button type="danger" style="width:48%;margin-top:10px" @click="handleTry()">获取体验号</el-button>
      </el-form>
    </el-card>
    <div style="font-size: 50px;font-family:YouYuan,serif;color:#135588;padding-top: 120px;text-align: center;">
      <span>除非我不想赢，不然没人能让我输</span>
    </div>
    <el-dialog :visible.sync="dialogVisible" :show-close="false" :center="true" width="30%">
      <div style="text-align: center">
        <span class="font-title-large" style="font-size: 18px">扫描二维码添加微信获取免费体验账号</span>
        <br>
        <br>
        <img :src="img2" style="width: 280px;height: 260px;" alt="">
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogConfirm">确定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import {login} from "@/api/user";

export default {
  data() {
    return {
      bg: require("@/assets/login-bg.png"),
      img1: require("@/assets/images/public.png"),
      img2: require("@/assets/images/public2.png"),
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {required: true, message: "账号不能为空"},
        ],
        password: [
          {required: true, message: "密码不能为空"},
        ],
      },
      src: '@/assets/images/login_center_bg.png',
      dialogVisible: false,
    }
  },
  methods: {

    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          // 调用后台接口
          login(this.loginForm.username, this.loginForm.password).then(response => {
            console.log(response.data)
            this.$message({
              type: 'success',
              message: '登录成功!'
            });
            this.$router.push({
              name: '首页'
            })
          })
          // 验证后端数据库
        } else {
          console.log('登录失败，用户名或密码错误');
          return false
        }
      })
    },
    handleTry() {
      this.dialogVisible = true
    },
    dialogConfirm() {
      this.dialogVisible = false;
    }
  }
}
</script>

<style scoped>

.login-form-layout {
  position: absolute;
  left: 0;
  right: 0;
  width: 580px;
  margin: 280px auto;
  border-top: 20px solid #409eff;
}

.login-container {
  background-color: #212b57;
  height: 100%
}

.login-title {
  color: #409eff;
  text-align: center;
  font-size: 20px;
}

.login-form {
  position: relative;
  width: 420px;
  max-width: 100%;
  margin: 0 auto;
}

.login-container >>> .el-form-item {
  margin-top: 30px;
}

.login-center-layout {
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  margin-top: 200px;
}

</style>
